package logic

import (
	"context"
	"time"

	"github.com/comfygpt-backend-go/common/consts"
	"github.com/comfygpt-backend-go/common/model"
	"github.com/comfygpt-backend-go/common/utils"
	"github.com/comfygpt-backend-go/service/component-rpc/component"
	"github.com/comfygpt-backend-go/service/component-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

type CreateComponentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateComponentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateComponentLogic {
	return &CreateComponentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateComponentLogic) CreateComponent(in *component.CreateComponentRequest) (*component.Empty, error) {
	newComponent := model.Components{
		Name:        in.Name,
		Description: in.Description,
		Category:    in.Category,
		Icon:        in.Icon,
		Tags:        utils.ArrayToString(in.Tags),
		CreateTime:  time.Now(),
		UpdateTime:  time.Now(),
	}

	err := l.svcCtx.ComponModel.Insert(l.ctx, &newComponent)
	if err != nil {
		return nil, status.Error(consts.ERROR_INTERNAL_SERVER_OPERATION, err.Error())
	}

	return &component.Empty{}, nil
}
